package cn.abcsys.cloud.devops.web.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.abcsys.cloud.devops.web.dao.AuthorityMapper;
import cn.abcsys.cloud.devops.web.dao.RoleActionMapper;
import cn.abcsys.cloud.devops.web.entity.Authority;
import cn.abcsys.cloud.devops.web.entity.GridBean;
import cn.abcsys.cloud.devops.web.entity.PagerModel;
import cn.abcsys.cloud.devops.web.entity.RoleAction;
import cn.abcsys.cloud.devops.web.entity.User;
import cn.abcsys.cloud.devops.web.service.AuthorityService;
/**
 * 权限服务实现类
 * @author xzg
 *
 */
@Component
public class AuthorityServiceImpl implements AuthorityService{
	private static final Logger LOGGER = Logger.getLogger(AuthorityServiceImpl.class);
	@Resource
	private AuthorityMapper authorityMapper;
	
	@Resource
	private RoleActionMapper roleActionMapper;
	
	@Override
	public List<Authority> getUserRoleAuths(Map<String,Integer> map) {
		try {
			map.put("action", 1);//增加查询菜单权限
			return authorityMapper.selectAuthoritiesByUserIdAndEnvId(map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * xzg
	 * @see cn.abcsys.cloud.devops.web.service.AuthorityService#getUserRoleAuths()
	 */
	@Override
	public List<Authority> getAdminRoleAuths(Map<String,?> map) {
		// TODO Auto-generated method stub
		//超级管理员的角色id为1
		return authorityMapper.selectAllAuthorityByRoleId(map);
	}

	@Override
	public GridBean getAuthoritys(Authority authority,HttpSession session, PagerModel pagerModel) {
		int page = pagerModel.getPage();
		int rows = pagerModel.getRows();
		int totalpage = 0;
		Long totalNum = 0l;
		Page<Authority> pageObject = new Page<Authority>();
		Map<String,Object> map = new HashMap<>();
		try {
			User user = (User)session.getAttribute("user");
			Integer envId = (Integer)session.getAttribute("envId");
			map.put("envId", envId);
			map.put("userId", user.getUserId());
			map.put("isAdmin", user.getIsAdmin());
		} catch (NullPointerException e) {
			// TODO: handle exception
			LOGGER.error("会话失效："+e.getMessage());
		}
		map.put("authorityName", authority.getAuthorityName());
		PageHelper.startPage(page, rows);
		List<Authority> authorityList = authorityMapper.getAuthoritys(map);
		for(Authority aution: authorityList){
			pageObject.add(aution);
		}
		totalpage = ((Page<?>) authorityList).getPages();
		totalNum = ((Page<?>) authorityList).getTotal();
		GridBean gridBean = new GridBean(page, totalpage, totalNum.intValue(), pageObject);
		return gridBean;
	}

	@Override
	public List<Authority> getUserButtonAuths(Map<String, Integer> map) {
		// TODO Auto-generated method stub
		try {
			map.put("action", 3);//增加查找按钮权限条件
			return authorityMapper.selectAuthoritiesByUserIdAndEnvId(map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int addRoleActions(List<RoleAction> raList) {
		// TODO Auto-generated method stub
		
		
		return 0;
	}

	@Override
	public int updateAction(Authority authority) throws SQLException {
		
		return authorityMapper.update(authority);
		
	}
	
}
